//
//  HNSH_UploadLocalAlert.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/8.
//

#import "HNSH_UploadLocalAlert.h"

#import "HNSH_PopupController.h"
#import <TZLocationManager.h>

#import "HNSH_TaskLocalViewController.h"

@interface HNSH_UploadLocalAlert ()

/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *nyPopView;

/* localLabel */
@property (nonatomic, strong) UILabel *localLabel;

/* location */
@property (nonatomic, strong) CLPlacemark *location;

@end

@implementation HNSH_UploadLocalAlert

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addAllViews];
}

- (void)showView {
    self.nyPopView = [[HNSH_PopupController alloc]init];
    self.nyPopView.dismissOnMaskTouched = YES;
    self.nyPopView.layoutType = PopupLayoutTypeCenter;
    self.nyPopView.slideStyle = PopupSlideStyleFade;
    [self.nyPopView presentContentView:self duration:0.25 springAnimated:YES inView:kSTWindow];
    self.nyPopView.popupView.layer.cornerRadius = 20.f;
    self.nyPopView.popupView.layer.masksToBounds = YES;
    
    [kSTWindow makeToast:@"归属地区只能修改一次"];
}

- (void)closeView {
    [self.nyPopView dismissWithDuration:0.25 springAnimated:YES];
}

- (void)addAllViews {
    self.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 18, 120, 22)];
    titleLabel.text = @"当前定位地址：";
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor appGrayLabelColor];
    [self addSubview:titleLabel];
    
    UILabel *desLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 94, 150, 22)];
    desLabel.text = @"确定为归属地定位？";
    desLabel.font = [UIFont systemFontOfSize:16];
    desLabel.textColor = [UIColor appGrayLabelColor];
    [self addSubview:desLabel];
    
    UILabel *localLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 56, self.width-40, 30)];
    localLabel.text = @"定位失败";
    localLabel.font = [UIFont boldSystemFontOfSize:20];
    localLabel.textColor = [UIColor appBlackLabelColor];
    [self addSubview:localLabel];
    self.localLabel = localLabel;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(23, 140, 120, 44);
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    leftButton.backgroundColor = [UIColor appBGColor];
    [self addSubview:leftButton];
    leftButton.layer.cornerRadius = 22;
    leftButton.layer.masksToBounds = YES;
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(158, 140, 120, 44);
    [rightButton setTitle:@"确定" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    rightButton.backgroundColor = [UIColor appGreenColor];
    [self addSubview:rightButton];
    rightButton.layer.cornerRadius = 22;
    rightButton.layer.masksToBounds = YES;
    
    UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeButton.frame = CGRectMake(self.width-92, 14, 92, 22);
    [changeButton setTitle:@"切换" forState:UIControlStateNormal];
    [changeButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
    changeButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:changeButton];
    
    __weak __typeof(&*self)weakSelf = self;
    [[TZLocationManager manager] startLocationWithGeocoderBlock:^(NSArray *geocoderArray) {
        CLPlacemark *location = geocoderArray.firstObject;
        weakSelf.location = location;
        weakSelf.localLabel.text = [NSString stringWithFormat:@"%@/%@/%@",location.administrativeArea,location.locality,location.subLocality];
    }];
    
    [[leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf closeView];
    }];
    
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadTaskUpdateLocal];
        [weakSelf closeView];
    }];
    
    [[changeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_TaskLocalViewController *vc = [[HNSH_TaskLocalViewController alloc]init];
        [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        [weakSelf closeView];
    }];
    
}

- (void)loadTaskUpdateLocal {
    if ([self.localLabel.text isEqualToString:@"定位失败"]) {
        [kSTWindow makeToast:@"请选择城市"];
        return;
    }
    WeakSelf(weakSelf);
    [HNSH_UserNetServer requestUpdateLocalWithLatitude:self.location.location.coordinate.latitude longitude:self.location.location.coordinate.longitude region_code:@"" success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [kSTWindow makeToast:@"更新归属地成功"];
            [[ST_UserManager shareInstance] loadUserInfoComplate:^{
                if (weakSelf.dismissBlock) {
                    weakSelf.dismissBlock();
                }
            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}


@end
